package com.example.rabbitmq01.topic;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Envelope;
import org.springframework.amqp.core.Message;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeoutException;

public class Producer {

    private static final String EXCHANGE_NAME = "TopicExchange";
    private static final String QUEUE_NAME01 = "Queue01";
    private static final String QUEUE_NAME02 = "Queue02";

    public static void main(String[] args) throws IOException, TimeoutException {
        //创建工厂类并使用默认配置
        ConnectionFactory factory = new ConnectionFactory();
        Connection connection = factory.newConnection();
        Channel channel =  connection.createChannel();

        //创建Topic类型的交换机
        channel.exchangeDeclare(EXCHANGE_NAME, "topic");

        //创建队列
        channel.queueDeclare(QUEUE_NAME01, false, false, false, null);
        channel.queueBind(QUEUE_NAME01, EXCHANGE_NAME, "*.aaa.bbb.#");

        channel.queueDeclare(QUEUE_NAME02, false, false, false, null);
        //可以匹配任意的

        channel.queueBind(QUEUE_NAME02, EXCHANGE_NAME, "#");

        Map<String, String> pair = new HashMap<>();
        pair.put("bbb.aaa.bbb", "1"); //queue1 queue2 都接收
        pair.put("bbb.aaa.bbb.ccc.ddd", "2"); //queue2 接收
        pair.put("aaa.aaa.ccc", "3"); //queue1 queue2 都接收

        pair.put("aaa.bbb.ccc", "4"); //queue2 接收
        pair.put("aaa.bbb", "5"); //queue2 接收
        pair.put("aaa", "6"); //queue2 接收

        for(Map.Entry<String,String> e : pair.entrySet()){
            String routineKey = e.getKey();
            String message = e.getValue();
            channel.basicPublish(EXCHANGE_NAME, routineKey, null, message.getBytes());
        }

        System.out.println("发送成功~");
    }
}
